//
// Created by Lenovo on 2020/8/26.
//

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1e5+10;
struct node{
    int u,v;
    int val;
    bool operator<(const node &a) const
    {
        return val<a.val;
    }
};
priority_queue<node>pqe;
int fa[maxn];
int find(int x)
{
    if(fa[x]==x) return x;
    return find(fa[x]);
}
void unite(int x,int y)
{
    x = find(x);
    y = find(y);
    if(x!=y)fa[y] = x;
}

void kruskal()
{
    int ans = 0;
    while(!pqe.empty())
    {
        node edge;
        edge = pqe.top();
        pqe.pop();
        int u = edge.u;
        int v = edge.v;
        int val = edge.val;
        if(find(u)!=find(v))
        {
            unite(u,v);
            //printf("********\n");
            ans += val;
        }
    }
    printf("%d\n",ans);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        while(!pqe.empty())pqe.pop();
        for(int i = 1;i<=n;i++) fa[i] = i;
        for(int i = 1;i<=m;i++)
        {
            node edge;
            scanf("%d %d %d",&edge.u,&edge.v,&edge.val);
            //  fa[edge.v] = edge.u;
            pqe.push(edge);
        }
        kruskal();

    }

}